package com.w1.security.domain;

import com.alibaba.fastjson.annotation.JSONField;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.jetbrains.annotations.NotNull;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import sun.font.TrueTypeFont;
import sun.plugin2.message.Serializer;

import java.security.Permission;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author mingyifan
 * @date 2022-02-15 15:44
 */
@Data
@NoArgsConstructor
public class LoginUser implements UserDetails {

    private User user;
    private List<String> premissions;

    public LoginUser(User user, List<String> premissions) {
        this.user = user;
        this.premissions = premissions;
    }
    @JSONField(serialize = false)
    List<SimpleGrantedAuthority> collect;
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if (collect!=null){
            return collect;
        }
        //把user的权限信息封装成GrantedAuthority的一个实现类SimpleGrantedAuthority
        collect = premissions.stream()
                .map(SimpleGrantedAuthority::new)
                .collect(Collectors.toList());

        return collect;
    }

    @Override
    public String getPassword() {
        System.out.println(user.getPassword());
        return user.getPassword();
    }

    @Override
    public String getUsername() {
        return user.getUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
